/********************************** (C) COPYRIGHT *******************************
 * File Name          : ch32v30x_it.c
 * Author             : WCH
 * Version            : V1.0.0
 * Date               : 2021/06/06
 * Description        : Main Interrupt Service Routines.
 *********************************************************************************
 * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
 * Attention: This software (modified or not) and binary are used for
 * microcontroller manufactured by Nanjing Qinheng Microelectronics.
 *******************************************************************************/
#include "ch32v30x_it.h"
#include "main.h"

void NMI_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void HardFault_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast")));

/*********************************************************************
 * @fn      NMI_Handler
 *
 * @brief   This function handles NMI exception.
 *
 * @return  none
 */
void NMI_Handler(void)
{
}

/*********************************************************************
 * @fn      HardFault_Handler
 *
 * @brief   This function handles Hard Fault exception.
 *
 * @return  none
 */
void HardFault_Handler(void)
{
    while (1)
    {
    }
}

void USART1_IRQHandler(void)
{
    uint8 data;

    if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
    {
        USART_ClearFlag(USART1, USART_IT_RXNE);
        data = (uint8)USART_ReceiveData(USART1);
        UartReceiveDataProcessing(data);
    }

    if (USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
    {
        USART_ClearFlag(USART1, USART_IT_TXE);
    }
}

void TIM1_UP_IRQHandler(void)
{
    if (TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET)
    {
        TIM_ClearFlag(TIM1, TIM_IT_Update);
        // ControlPeriodTask();
        if (0 == stepper_motor_controller.timer_tick % stepper_motor_controller.n_multiple_of_timmer_tick)
        {
            stepper_motor_controller.isr_run_num++;
        }

        // DBprintf("%d %#x\r\n", stepper_motor_controller.isr_run_num, stepper_motor_controller.timer_tick);

        stepper_motor_controller.timer_tick++;
        if (stepper_motor_controller.timer_tick >= UINT32_MAX / stepper_motor_controller.n_multiple_of_timmer_tick * stepper_motor_controller.n_multiple_of_timmer_tick)
        // if (stepper_motor_controller.timer_tick >= UINT32_MAX)
        {
            stepper_motor_controller.timer_tick = 0;
        }
    }
}

void EXTI9_5_IRQHandler(void)
{
    if (EXTI_GetITStatus(EXTI_Line7) != RESET)
    {
        EXTI_ClearITPendingBit(EXTI_Line7);
        
        /* dir processing */
        // stepper_motor_controller.step_controller.dir_pin = stepper_motor_controller.mcu->GpioRead(stepper_motor_controller.driver_board->param_pin_DIR_in.pin);
        if (stepper_motor_controller.step_controller.dir_pin != 0)
        {
            stepper_motor_controller.mcu->SetInputPulseCouterCountUp();
        }
        else
        {
            stepper_motor_controller.mcu->SetInputPulseCouterCountDown();
        }
    }
}

// void TIM8_UP_IRQHandler(void)
// {
//     // DBprintf("r:%d\r\n", TIM8->SWEVGR);
//     if ((TIM8->INTFR & TIM_IT_Update))
//     {
//         GPIOE->BSHR = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
//         TIM8->INTFR = (uint16_t)~TIM_IT_Update;
//     }
// }

// void TIM8_CC_IRQHandler()
// {
//     if ((TIM8->INTFR & TIM_IT_CC1))
//     {
//         GPIOE->BCR = GPIO_Pin_8;
//         TIM8->INTFR = (uint16_t)~TIM_IT_CC1;
//     }
//     else if ((TIM8->INTFR & TIM_IT_CC2))
//     {
//         GPIOE->BCR = GPIO_Pin_9;
//         TIM8->INTFR = (uint16_t)~TIM_IT_CC2;
//     }
//     else if ((TIM8->INTFR & TIM_IT_CC3))
//     {
//         GPIOE->BCR = GPIO_Pin_10;
//         TIM8->INTFR = (uint16_t)~TIM_IT_CC3;
//     }
//     else if ((TIM8->INTFR & TIM_IT_CC4))
//     {
//         GPIOE->BCR = GPIO_Pin_11;
//         TIM8->INTFR = (uint16_t)~TIM_IT_CC4;
//     }
// }